import baseView from '../../../../app/base/baseView';

const { ccclass, property } = cc._decorator;

interface callback {
    (obj: { confirm: boolean, cancel: boolean }): void
}

interface onShow { title?: string, content?: string, showCancel?: boolean, cancelText?: string, cancelColor?: string, confirmText?: string, confirmColor?: string, success?: callback, fail?: callback, complete?: callback }

@ccclass
export default class roofModal extends baseView {
    @property(cc.Label)
    private titleLab: cc.Label = null;
    @property(cc.Label)
    private contentLab: cc.Label = null;

    @property(cc.Button)
    private cancelBtn: cc.Button = null;
    @property(cc.Label)
    private cancelTextLab: cc.Label = null;

    @property(cc.Button)
    private confirmBtn: cc.Button = null;
    @property(cc.Label)
    private confirmTextLab: cc.Label = null;

    @property(cc.Button)
    private sureBtn: cc.Button = null;
    @property(cc.Label)
    private sureTextLab: cc.Label = null;

    private success: callback = null;
    private fail: callback = null;
    private complete: callback = null;

    private confirm() {
        this.hide(true);
        if (this.success) {
            this.success({ confirm: true, cancel: false });
        }
        if (this.complete) {
            this.complete({ confirm: true, cancel: false });
        }
    }
    private cancel() {
        this.hide(false);
        if (this.success) {
            this.success({ confirm: false, cancel: true });
        }
        if (this.complete) {
            this.complete({ confirm: false, cancel: true });
        }
    }
    private forceUpdateLabelStr(label, str) {
        label.string = str;
        label._updateRenderData ? label._updateRenderData(true) : label._forceUpdateRenderData(true);
    }

    protected onLoad() {
        this.sureBtn.node.on('click', this.confirm, this);
        this.confirmBtn.node.on('click', this.confirm, this);
        this.cancelBtn.node.on('click', this.cancel, this);
    }

    /**
     * https://developers.weixin.qq.com/minigame/dev/document/ui/interaction/wx.showModal.html
     */
    protected onShow({ title = '', content = '', showCancel = true, cancelText = '取消', cancelColor = '#000000', confirmText = '确认', confirmColor = '#3cc51f', success, fail, complete }: onShow = {}) {
        this.success = success;
        this.fail = fail;
        this.complete = complete;

        this.forceUpdateLabelStr(this.titleLab, title);
        this.forceUpdateLabelStr(this.contentLab, content);

        if (showCancel) {
            this.cancelBtn.node.active = true;
            this.confirmBtn.node.active = true;
            this.sureBtn.node.active = false;

            this.cancelTextLab.node.color = cc.color().fromHEX(cancelColor);
            this.confirmTextLab.node.color = cc.color().fromHEX(confirmColor);
            this.forceUpdateLabelStr(this.cancelTextLab, cancelText);
            this.forceUpdateLabelStr(this.confirmTextLab, confirmText);
        } else {
            this.cancelBtn.node.active = false;
            this.confirmBtn.node.active = false;
            this.sureBtn.node.active = true;

            this.sureTextLab.node.color = cc.color().fromHEX(confirmColor);
            this.forceUpdateLabelStr(this.sureTextLab, confirmText);
        }
    }
}
